package de.fhkl.gatav.ut.framedancer_4;

import de.fhkl.gatav.ut.framedancer_4.R;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class FrameDancer_4_Activity extends Activity {
	private ImageView animView;
	private Boolean isRunning; 
	private AnimationDrawable frameAnim;
	private AnimationDrawable standStillAnim;
	private AnimationDrawable danceLeftAnim;
	private AnimationDrawable danceRightAnim;
	private AnimationDrawable jumpAnim;
	private AnimationDrawable myChoreo;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Load view and default (almost empty) animation resource
		animView = (ImageView) findViewById(R.id.danceView);
		frameAnim = (AnimationDrawable) animView.getDrawable();
		isRunning = true;
	
		//Load scene dance steps
		standStillAnim = (AnimationDrawable) getResources().getDrawable(R.anim.standstill);
		danceLeftAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_left);
		danceRightAnim = (AnimationDrawable) getResources().getDrawable(R.anim.dance_right);
		jumpAnim = (AnimationDrawable) getResources().getDrawable(R.anim.jump);

		//Compile scenes to dance
		frameAnim.setOneShot( false );
		attachAnim( frameAnim, danceLeftAnim, 1, 12 );
		attachAnim( frameAnim, danceRightAnim, 1, 11 );
		attachAnim( frameAnim, jumpAnim, 1, 11 );
		
		//Setting the frame animation into the view preloads it. 
		//So after getting it again it should play without stucking
		animView.setImageDrawable(frameAnim);
		frameAnim = (AnimationDrawable)animView.getDrawable();
		frameAnim.setVisible(true,true);	

		//Start dance
		animView.post( 
			new Runnable() {
				public void run() { 
					frameAnim.start();
				}
			}
		);
    }
    
    public void onClick_dance(View view) {
		AnimationDrawable frameAnimation;
		frameAnimation = (AnimationDrawable)animView.getDrawable();
		final Button danceToggle = (Button) findViewById(R.id.danceToggle);

		//if running --> stop
		if (isRunning) {
			frameAnimation.stop();
			isRunning = false;
			danceToggle.setText(R.string.dance_text);
		//if stoped --> start	
		} else {
			frameAnimation.start();
			isRunning = true;
			danceToggle.setText(R.string.stop_text);
		}
    }
    
    public void onClick_doMyChoreo(View view) {
		final EditText editField = (EditText) findViewById(R.id.editText_movesToLearn);
		String text = new String( editField.getEditableText().toString() );
		text.toUpperCase();
		char c;
		int i;
		
		//Switch button to stop mode
		final Button danceToggle = (Button) findViewById(R.id.danceToggle);		
		isRunning = false;
		danceToggle.setText(R.string.dance_text);		
		
		//Stop animation
		AnimationDrawable oldFrameAnimation;
		oldFrameAnimation = (AnimationDrawable)animView.getDrawable();
		oldFrameAnimation.stop();

		//Create animDrawable object an bind to resource
		myChoreo = new AnimationDrawable();
		myChoreo = standStillAnim;
		
		//Compile steps into choreography
		for (i=0; i<text.length(); i++) {
			c = text.charAt(i);
			
			switch (c) {
			case 'R':
				attachAnim( myChoreo, danceRightAnim, 1, 11 );
				break;
			case 'L':
				attachAnim( myChoreo, danceLeftAnim, 1, 12 );
				break;
			case 'J':
				attachAnim( myChoreo, jumpAnim, 1, 11 );
				break;
			}
		}

		//Set new choreography to the view
		animView.setImageDrawable(myChoreo);
		myChoreo.setOneShot(false);
		//setVisible(false,true) restarts animation: so the rest of the old one animation is not played anymore		
		myChoreo.setVisible(false,true);
    }
    
	private void attachAnim( AnimationDrawable destAnim, AnimationDrawable srcAnim, int start, int end ) {
		for (int i = start; i<=end; ++i) {
			destAnim.addFrame( srcAnim.getFrame (i), srcAnim.getDuration (i) );
		}
	}
}